var stationLayer;
var returnValue;
function showValue(name,id) {
	$("#"+returnValue).val(name);
	$("#"+returnValue).attr("name",id);
	if(!isEmpty(stationLayer)){
		stationLayer.closeAll();
		stationLayer = null;
	}	
	$('.bx-controls-direction').show();
}

function showStation(obj) {
	$('.bx-controls-direction').hide();
	returnValue = $(obj).attr("id");
	document.getElementById('chooseStation');
	var titleStyle = 'font-size:14px; font-weight:bold; background-color:#71aa27;color:white;height:30px;line-height:30px;';
	var content = $('#chooseStation').html();
	fixedLayer('请选择站点', titleStyle, content, '600px');
}


function lineQuery(prefix,nodeUrl){
	$('.bx-controls-direction').hide();
	var startId = $("#start").attr("name");
	var endId = $("#end").attr("name");
	
	if(isEmpty(startId)){
		alert("请选择起始站名称！");
		return;
	}
	
	if(isEmpty(endId)){
		alert("请选择终点站名称！");
		return;
	}
	
	lineQueryResult(startId, endId,prefix,nodeUrl);
}

function lineQueryResult(startId, endId,prefix,nodeUrl){
	jQuery.ajax({
	    url: prefix+"/ext/ticketPrice.htmx?startId="+startId+"&endId="+endId,
	    success: function (obj) {
	    	if (obj.info.success) {
				if (obj.data != null && obj.data.length > 0) {
					var data = obj.data[0];
					startStationSeq = data.startStation.seq;
					endStationSeq = data.endStation.seq;
					var stationCount=(startStationSeq>endStationSeq)?(startStationSeq-endStationSeq):(endStationSeq-startStationSeq);
					$("#startStation").text(data.startStation.name);
					$("#endStation").text(data.endStation.name);
					$("#stationCount").text(stationCount);
					$("#ticketPrice").text(data.price);
					
					$("#lineTime").empty();
					var timeHtml="";
					$.ajax({
						url : prefix+"/ext/schedule.htmx?stationId="+startId,
						type : 'GET',
						async : false,
						error : function() {
							alert('系统连接失败，请稍后再试！');
						},
						success : function(result) {
							if (result.info.success) {
								if (result.data != null && result.data.length > 0) {
									for (var i = 0; i < result.data.length; i++) {
										var tmpDate = result.data[i];
										timeHtml+="<ul>"
										timeHtml+="<li class=\"clearfix\">"
										timeHtml+="<span class=\"span03\">"+tmpDate.line.id+"</span>"
										timeHtml+="<span class=\"station-name\">"+tmpDate.name+"</span>"
										timeHtml+="<span class=\"station-direction\">("+tmpDate.toDirection+")</span></li>"
										
										timeHtml+="<li class=\"station-time clearfix\">";
										timeHtml+="<span class=\"span04\">首</span>";
										timeHtml+="<span class=\"time01\">"+tmpDate.workdayStartTo+"</span></li>";
										
										timeHtml+="<li class=\"station-time clearfix\">";
										timeHtml+="<span class=\"span04\">末</span>";
										timeHtml+="<span class=\"time01\">"+tmpDate.workdayEndTo+"</span></li></ul>";
									
										timeHtml+="<ul>"
										timeHtml+="<li class=\"clearfix\">"
										timeHtml+="<span class=\"span03\">"+tmpDate.line.id+"</span>"
										timeHtml+="<span class=\"station-name\">"+tmpDate.name+"</span>"
										timeHtml+="<span class=\"station-direction\">("+tmpDate.comeDirection+")</span></li>"
										
										timeHtml+="<li class=\"station-time clearfix\">";
										timeHtml+="<span class=\"span04\">首</span>";
										timeHtml+="<span class=\"time01\">"+tmpDate.workdayStartCome+"</span></li>";
										
										timeHtml+="<li class=\"station-time clearfix\">";
										timeHtml+="<span class=\"span04\">末</span>";
										timeHtml+="<span class=\"time01\">"+tmpDate.workdayEndCome+"</span></li></ul>";
										timeHtml += "<div style = \"float:right;\"><a href = \""+nodeUrl+"?startId="+startId+" \" target=\"_blank\">";
										timeHtml += "<span style = \"color:#71aa27;\">查看详细运营时间</span></a></div>";
									}
								}
							}
						}
					});
					$("#lineTime").append(timeHtml);
				}
			}
	    	
	    	var titleStyle = 'font-size:14px; font-weight:bold; background-color:#71aa27;color:white;height:30px;line-height:30px;';
	    	var content = $('#queryResult').html();
	    	fixedLayer('查询结果', titleStyle, content, '600px');
	    }
	});
	$('.bx-controls-direction').show();
/*art.dialog({
		id:"queryResult",
		title:"查询结果",
	    content: document.getElementById('queryResult'),
	    lock: true,
	    beforeunload: function () {
	        $('.bx-controls-direction').show();
	    }
	});*/
	
	//var queryResult = document.getElementById('queryResult').innerHTML;
}

function stationQuery(prefix){
	var stationId = $("#station").attr("name");
	if(isEmpty(stationId)){
		alert("请选择站点名称！");
		return;
	}
	$('.bx-controls-direction').hide();
	stationQueryResult(prefix, stationId);
}

function stationQueryResult(prefix, stationId){
	/*var myDialog = art.dialog({
	    title: '站点详情',
//	    icon: 'succeed',
	    lock:true,
	    beforeunload: function () {
//	        return confirm('确定关闭对话框吗');
	        $('.bx-controls-direction').show();
	    }
	});// 初始化一个带有loading图标的空对话框
*/	
	
	$.ajax({
		type : 'get',
		async : false,
		url : prefix+'/ext/entryInfo.htmx?stationId='+stationId,
		dataType : 'json',
		error : function() {
			alert('系统连接失败，请稍后再试！');
		},
		success : function(result) {
			if (result.info.success){
				if (result.data != null && result.data.length > 0) {
					$(".chukouzhiyin").empty();
					var html="";
					for (var i = 0; i < result.data.length; i++) {
						var entryInfo = result.data[i];
						var entryInfoDetails = entryInfo.entryInfoDetails;
						html+="<div class=\"zd-zhiyin\" style=\"border-bottom: 1px solid #ccc;\">"
						html+="<p class=\"zd-chukou\"><span class=\"chukou\">"+entryInfo.name+"出口</span></p>"
						html+="<ul class=\"zyxq\">"
							$.each(entryInfoDetails,function(k,v){
								html+="<li>"+v.name+"</li>"
							});
						html+="</ul></div>"
					}

					html += "<div class=\"zd-zhiyin\"><p class=\"zd-chukou\"><span class=\"chukou\">商铺信息:</span></p>";
					html += "<ul class=\"zyxq\"><li>暂无商铺信息</li></ul></div>";
					$(".chukouzhiyin").append(html);
					
					var titleStyle = 'font-size:14px; font-weight:bold; background-color:#71aa27;color:white;height:30px;line-height:30px;';
			    	fixedLayer('站点详情', titleStyle, html, '600px');
					/*myDialog.content(html);// 填充对话框内容
*/				}
			}
		}
		
	});
	$('.bx-controls-direction').show();
}

/**
 * 判断表单中的内容是否为空或者未定义
 * @param obj
 * @returns {Boolean}
 */
function isEmpty(obj){
	if(obj=="" || obj==null || obj=="undefined"){
		return true;
	}else{
		return false;
	}
}

//js获取项目根路径，如： http://localhost:8083/uimcardprj
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    return localhostPaht;
}